package com.wiz.web.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wiz.web.dao.mapper.OilTankMapper;
import com.wiz.web.service.OilTankService;
import com.wiz.web.vo.criteria.FpCriteria;
import com.wiz.web.vo.criteria.OilTankCriteria;
import com.wiz.web.vo.to.PageTO;
import com.wiz.web.vo.to.StationTO;

@Service("oilTankService")
public class OilTankServiceImpl extends BasicServiceImpl implements OilTankService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private OilTankMapper oilTankMapper;

	@Override
	public String activateOilTank(OilTankCriteria criteria) throws Exception{
		int result = oilTankMapper.activateOilTank(criteria);
		if(result > 0){
			log.info("stationId="+criteria.getStationId()+" activateOilTank...........success");
			return RESPONSE_CODE_OK;
		}
		log.warn("stationId="+criteria.getStationId()+" activateOilTank...........fail");
		return RESPONSE_CODE_ACTIVATE_ERROR;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public String oilTankMonitor(String stationId,List<OilTankCriteria> criteriaList) throws Exception {
		StationTO station = oilTankMapper.getStationsByStationId(stationId);
		for(OilTankCriteria oilTank:criteriaList){
			oilTank.setHqId(station.getHqId());
			oilTank.setHqName(station.getHqName());
			oilTank.setStationName(station.getStationName());
			oilTankMapper.insertOilTankMonitor(oilTank);
		}
		return RESPONSE_CODE_OK;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public String configOilTank(String stationId,List<OilTankCriteria> criteriaList) throws Exception {
		StationTO station = oilTankMapper.getStationsByStationId(stationId);
		for(OilTankCriteria criteria:criteriaList){
			criteria.setHqId(station.getHqId());
			criteria.setHqName(station.getHqName());
			criteria.setStationName(station.getStationName());
			int result = oilTankMapper.getCntOilTankConfig(criteria);
			if(result>0){
				oilTankMapper.updateOilTankConfig(criteria);
			}else{
				oilTankMapper.insertOilTankConfig(criteria);
			}
		}
		return RESPONSE_CODE_OK;
	}

	@Override
	public String getFPByStationPR(FpCriteria criteria) throws Exception{
		PageTO page = new PageTO();
		page.setDataList(oilTankMapper.getFPByStationPR(criteria));
		return returnResult(CODE_OK, STRING_OK, page);
	}
}
